
//求二叉树中两个结点的公共祖先

class Solution1 {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root==null){
            return null;
        }
        if(root==p||root==q){
            return root;
        }
        TreeNode leftRet=lowestCommonAncestor(root.left,p,q);
        TreeNode rightRet=lowestCommonAncestor(root.right,p,q);
        if(leftRet!=null&&rightRet!=null){
            return root;
        }else if(leftRet!=null){//如果一个点在根的左边，一个在根的右边，要是先走到跟左边的结点p，直接返回了这个结点，就没有机会遍历到q这个节点了,相当于为空，
            return leftRet;
        }else if(rightRet!=null){
            return rightRet;
        }
        return  null;

        
    }
}